﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_CheckCharacterName
    /// </summary>
    public class DB_CheckCharacterName : IDBProc
    {
        private string m_Name;
        private TCPConnection m_client;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            Count = 0,
        }
        public DB_CheckCharacterName(TCPConnection client, string szName)
        {
            m_client = client;
            m_Name = szName;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            string marker;
            DbParameter name = GameDBManager.Instance.CreateParameter("IN_NAME", out marker);
            cmd.CommandText = String.Format("SELECT COUNT(*) AS ct FROM Character WITH (NOLOCK) WHERE name={0}", marker);
            cmd.CommandType = CommandType.Text;
            name.DbType = DbType.String;
            name.Direction = ParameterDirection.Input;
            name.Value = m_Name;
            cmd.Parameters.Add(name);
            conn.Open();
            //            cmd.Prepare();

            DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());

            ushort count = 0;

            try
            {
                if (dr.Read())
                {
                    count = (ushort)dr.GetInt32(0);
                }
            }
            catch(Exception ex)
            {
                Globals.Log.Error("DB_CheckCharacterName Exception: {0}", ex);
            }

            conn.Close();

            Messages.SendResult(m_client, 2006, (ushort)(count == 0 ? 0 : 9), 0);

            return true;
        }
    }
}
